﻿using huqiang;
using huqiang.Communication;
using huqiang.Core.HGUI;
using huqiang.Framework;
using huqiang.Framework.Http;
using huqiang.UIModel;
using UnityEngine;

public class GameSystem : MonoBehaviour
{
    static GameSystem Instance;
    public string RemoteIP = "192.168.0.134";
    public int RemotePort = 8899;
    public TextAsset baseUI;
    public bool HotFix;
    /// <summary>
    /// 远程配置
    /// </summary>
    public string url = "url0";
    private void Awake()
    {
        if (Instance == null)
        {
            Instance = this;
            DontDestroyOnLoad(gameObject);
        }
        else
        {
            Destroy(gameObject);
        }
    }
    // Start is called before the first frame update
    void Start()
    {
        if (Instance == this)
        {
            HFBase.AddModule<HttpManager>();
            HFBase.AddModule<MainThreadMissionManager>();
            var kcp = HFBase.AddModule<KcpManager>();
            kcp.SetClientFactory<NetLog.LogUser>();
            kcp.server.Run(0, 256);
            Initial();
        }
    }
    private void Initial()
    {
        LocalSetting.Initial(url);
        var gui = GameObject.FindObjectOfType<HGUIRender>();
        App.Initial(gui.canvas);
        HGUIManager.LoadModels(baseUI.bytes, "baseUI");
#if UNITY_EDITOR
        AssetBundle.UnloadAllAssetBundles(true);
#endif
        UIPage.LoadPage<LoadingPage>().Cmd("checkVersion", HotFix);
    }

    // Update is called once per frame
    void Update()
    {
        if(Instance==this)
        {
            HFBase.UpdateAll(Time.unscaledDeltaTime);
        }
    }
    private void LateUpdate()
    {
        if (Instance == this)
        {
            HFBase.LateUpdateAll();
        }
    }
    private void OnDestroy()
    {
        if (Instance == this)
        {
            HFBase.ReleaseModule<HttpManager>();
            HFBase.ReleaseModule<KcpManager>();
        }
    }
}
